#include "link/include/client.h"
int get_command(puser_t pusr,poptdata_t poptd)
{
    printf("get command\n");
    bzero(poptd,sizeof(optdata_t));
    char command[128]={0};
    char com[8]={0};
    int i,j;
    printf("\033[34m%s@netdisc:%s #  \033[0m",pusr->username,pusr->path);
    fflush(stdout);
    read(STDIN_FILENO,command,sizeof(command));
    fflush(stdin);
    if('\n'==command[0])
    {
        return 0;
    }
    else
    {
        for(i=0;*(command+i)!=' '&&*(command+i)!='\n';++i)
        {
            *(com+i)=*(command+i);
        }
        if(' '==*(command+i))//带参数的命令
        {
            i+=1;
            for(j=0;*(command+i)!='\n'&&*(command+i)!=' ';++i,++j)
            {//获取参数
                *(poptd->arg+j)=*(command+i);
            }
            if(' '==*(command+i))
            {
                return -1;
            }
            if(0==strcmp(com,"cd"))
            {
                poptd->opt=1;
                return 1;
            }else if(0==strcmp(com,"puts"))
            {
                poptd->opt=3;
                return 3;
            }else if(0==strcmp(com,"gets"))
            {
                poptd->opt=4;
                return 4;
            }else if(0==strcmp(com,"remove"))
            {
                poptd->opt=5;
                return 5;
            }else if(0==strcmp(com,"mkdir"))
            {
                poptd->opt=7;
                return 7;
            }else
            {
                return -1;
            }

        }
        else if('\n'==*(command+i))//不带参数的命令
        {
            if(0==strcmp(com,"ls"))
            {
                poptd->opt=2;
                return 2;
            }else if(0==strcmp(com,"pwd"))
            {
                poptd->opt=6;
                return 6;
            }else if(0==strcmp(com,"clear"))
            {
                system("clear");
                return 0;
            }else if(0==strcmp(com,"quit"))
	    {
		    printf("Bye!\n");
		_exit(0);//退出客户端
	    }else
            {
                return -1;
            }
        }
    }
    return 0;
}
int do_command(int sockfd,optdata_t optd,puser_t pusr)
{
    switch(optd.opt){
    case 1:
        return do_cd(sockfd,optd,pusr);
    case 2:
        return do_ls(sockfd,optd);
    case 3:
        return do_puts(sockfd,optd);
    case 4:
        return do_gets(sockfd,optd);
    case 5:
        return do_remove(sockfd,optd);
    case 6:
        return do_pwd(sockfd,optd);
    case 7:
        return do_mkdir(sockfd,optd);
    default:
        return -1;
    }
    return 0;
}
int do_cd(int sockfd,optdata_t optd,puser_t pusr)
{
    optd.len=strlen(optd.arg);
    send(sockfd,&optd,8+optd.len,0);
    int len;
    recv(sockfd,&len,4,0);
    if(0==len)
    {//若不存在目标文件夹，返回长度为0的字符串
        printf("No such directory\n");
    }else
    {
        bzero(pusr->path,sizeof(pusr->path));
        recv(sockfd,pusr->path,len,0);//Server concatenate the path and then send it to client
    }
    return 0;
}
int do_ls(int sockfd,optdata_t optd)
{
    optd.len=0;//发送命令
    send(sockfd,&optd,8+optd.len,0);
    int len;
    char buf[4096]={0};
    recv(sockfd,&len,4,0);
    if(0==len)
    {
        printf("This directory is empty\n");
        return 0;
    }
    recv(sockfd,buf,len,0);
    printf("%s",buf);
    return 0;
}
int do_puts(int sockfd,optdata_t optd)
{
    return trans_file(sockfd,optd); 
}
int do_gets(int sockfd,optdata_t optd)
{
    optd.len=strlen(optd.arg);
    send(sockfd,&optd,8+optd.len,0);//发送命令
    int ret;
    recv(sockfd,&ret,4,0);
    if(0==ret)
    {//表示服务器端没有该文件
        printf("No such file!\n");
    }else{
        recv_file(sockfd,optd.arg);
    }
    return 0;
}
int do_remove(int sockfd,optdata_t optd)
{
    optd.len=strlen(optd.arg);
    send(sockfd,&optd,8+optd.len,0);
    int ret;
    recv(sockfd,&ret,4,0);
    if(-1==ret)printf("Do not support deleting directory right now!\n");
    else if(1==ret)printf("No such file!\n");
    else printf("Delete succeed!\n");
    return 0;
}
int do_pwd(int sockfd,optdata_t optd)
{
    optd.len=0;
    send(sockfd,&optd,8+optd.len,0);
    int len;
    char buf[1024]={0};
    recv(sockfd,&len,4,0);
    recv(sockfd,buf,len,0);
    printf("cwd:%s\n",buf);
    return 0;
}
int do_mkdir(int sockfd,optdata_t optd)
{
    optd.len=strlen(optd.arg);
    send(sockfd,&optd,8+optd.len,0);
    int ret;
    recv(sockfd,&ret,4,0);
    if(-1==ret)
    {
        printf("Repeat DIRname!\n");
    }else if(0==ret)
    {
        printf("Mkdir success\n");
    }
    return 0;
}
